package com.bristua.mall.user.biz;

import com.bristua.mall.common.exception.BizException;
import com.bristua.mall.common.utils.CacheUtil;
import com.bristua.mall.common.utils.DateUtil;
import com.bristua.mall.common.utils.DateUtils;
import com.bristua.mall.user.dao.TUserMapper;
import com.bristua.mall.user.dao.TuserForUpdateMapper;
import com.bristua.mall.user.enums.UserCode;
import com.bristua.mall.user.model.TUser;
import com.bristua.mall.user.model.condition.TUserExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2019/3/25.
 */
@Service
@Transactional
public class UpdateUserInviteCodeBiz {

    @Resource
    private TUserMapper tUserMapper;

    @Resource
    private TuserForUpdateMapper tuserForUpdateMapper;

    @Autowired
    private CacheUtil cacheUtil;

    public void updateUserInviteCodeBiz(String inviteCode) {
        TUser userSession = (TUser) cacheUtil.getUserSession();
        if(inviteCode.equals(userSession.getUserCode())){
            throw new BizException(UserCode.USER_INVITE_CODE_NOT_SELF.getCode(), UserCode.USER_INVITE_CODE_NOT_SELF.getMsg());
        }
        userSession = tUserMapper.selectByPrimaryKey(userSession.getUserId());
        if("0".equals(userSession.getUserLevel())){
            String parentId = userSession.getParentId();
            TUser tUser = tUserMapper.selectByPrimaryKey(parentId);
            if(tUser!=null){
                throw new BizException(UserCode.USER_NOT_UPDATE_PARENT_USER.getCode(), UserCode.USER_NOT_UPDATE_PARENT_USER.getMsg());
            }
        }else if("1".equals(userSession.getUserLevel())){
            String parentId = userSession.getParentId();
            TUser tUser = tUserMapper.selectByPrimaryKey(parentId);
            if(tUser!=null && !StringUtils.isEmpty(tUser.getUpdateCodeTime())){
                throw new BizException(UserCode.VIP_USER_NOT_UPDATE_PARENT_USER.getCode(), UserCode.VIP_USER_NOT_UPDATE_PARENT_USER.getMsg());
            }
        }
        String userId = userSession.getUserId();
        TUserExample tUserExample = new TUserExample();
        tUserExample.createCriteria().andUserCodeEqualTo(inviteCode);
        List<TUser> tUsers = tUserMapper.selectByExample(tUserExample);
        if (tUsers == null || tUsers.size() == 0) {
            throw new BizException(UserCode.USER_INVITE_CODE_NOT_FIND.getCode(), UserCode.USER_INVITE_CODE_NOT_FIND.getMsg());
        }
        TUser tUser1 = tUsers.get(0);
        if(String.valueOf(userSession.getParentId()).equals(tUser1.getUserId())){
            throw new BizException(UserCode.USER_HAS_CHANGE_INVITE.getCode(), UserCode.USER_HAS_CHANGE_INVITE.getMsg());
        }

        if ("0".equals(tUser1.getUserLevel())) {
            tuserForUpdateMapper.queryUserForUpdate(userSession.getUserId());//进行锁住
            TUserExample tUserExample1 = new TUserExample();
            tUserExample1.createCriteria().andParentIdEqualTo(tUser1.getUserId());
            int i = tUserMapper.countByExample(tUserExample1);
            if (i > 0) {
                throw new BizException(UserCode.NOT_VIP_USER_ONE.getCode(), UserCode.NOT_VIP_USER_ONE.getMsg());
            }
        }
        TUser tUser = tUserMapper.selectByPrimaryKey(userId);
        if("1".equals(tUser.getUserLevel())){
            String parentId = userSession.getParentId();
            TUser parentUser = tUserMapper.selectByPrimaryKey(parentId);
            if(parentUser!=null && StringUtils.isEmpty(parentUser.getUpdateCodeTime())){
                tUser.setUpdateCodeTime(DateUtil.getDateTime3());
            }
        }
        tUser.setParentId(tUser1.getUserId());
        tUserMapper.updateByPrimaryKeySelective(tUser);
    }
}
